using System;
using AntEater.ComponentModel;

namespace AntEater.Runtime.UI
{
    /// <summary>
    /// Presenter for the Build File View.
    /// </summary>
    public class BuildFilePresenter : PresenterBase, IBuildFilePresenter
    {
        /// <summary>
        /// Occurs when a file shoud be opened in a specific view.
        /// </summary>
        public event EventHandler<BuildFileViewEventArgs> OpenFile;

        /// <summary>
        /// Initializes a new instance of the <see cref="BuildFilePresenter"/> class.
        /// </summary>
        public BuildFilePresenter()
            : base(new BuildFileView()) {
        }

        /// <summary>
        /// Initializes this instance.
        /// </summary>
        /// <param name="context">The context.</param>
        public override void Initialize(PresenterContext context) {
            base.Initialize(context);

            this.View.OpenFile += new EventHandler<BuildFileViewEventArgs>(ViewOpenFile);
        }

        /// <summary>
        /// Gets the view.
        /// </summary>
        /// <value>The view.</value>
        public new BuildFileView View {
            get { return (BuildFileView)base.View; }
            protected set {
                BuildFileView view;

                //Remove Event Handlers form old view
                view = base.View as BuildFileView;
                if (view != null) {
                    view.OpenFile -= new EventHandler<BuildFileViewEventArgs>(ViewOpenFile);
                }

                view = value;
                base.View = view;

                //Add Event Handlers to new view
                if (view != null) {
                    view.OpenFile += new EventHandler<BuildFileViewEventArgs>(ViewOpenFile);
                }
            }
        }

        /// <summary>
        /// Gets the build file.
        /// </summary>
        /// <value>The build file.</value>
        public BuildFile BuildFile {
            get { return this.View.BuildFile; }
            set {
                this.View.BuildFile = value;
                this.Refresh();
            }
        }

        /// <summary>
        /// Called when [open file].
        /// </summary>
        /// <param name="buildFilePath">The build file path.</param>
        /// <param name="view">The view.</param>
        protected void OnOpenFile(string buildFilePath, BuildFileViewType view) {
            if (this.OpenFile != null) {
                this.OpenFile(this, new BuildFileViewEventArgs(buildFilePath, view));
            }
        }

        /// <summary>
        /// Views the open file.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="T:AntEater.Runtime.UI.BuildFileViewEventArgs"/> instance containing the event data.</param>
        private void ViewOpenFile(object sender, BuildFileViewEventArgs e) {
            this.OnOpenFile(e.BuildFilePath, e.View);
        }
    }
}